package com.sharejoys.propertyanimationdemo;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.graphics.PointF;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.BounceInterpolator;
import android.view.animation.CycleInterpolator;
import android.view.animation.OvershootInterpolator;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    ImageView mImg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mImg = findViewById(R.id.circle);
    }

    int position = 0;

    public void startAnimation(View view) {
        //view动画
//        Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate);
//        mImg.startAnimation(animation);

        //属性动画
//        position+=100;
//        mImg.setTranslationY(position);
//        mImg.setAlpha((float) Math.random());


        //单个动画执行
        //float .. values  float... values: A set of values that the animation will animate between over time.
//        ObjectAnimator oa = ObjectAnimator.ofFloat(view, "translationY", 0f, 300f);
//        oa.setDuration(500);
//        oa.start();

//        ObjectAnimator oaY = ObjectAnimator.ofFloat(view, "translationX", 0, 300);
//        oaY.setDuration(500);
//        oaY.start();

//        ObjectAnimator oa = ObjectAnimator.ofFloat(view, "rotation", 0f, 90f,180f);
//        oa.setDuration(500);
//        oa.start();

        //多个动画同时执行
        //方式一，设置动画监听，开始第一个动画的同时开启其他动画
//        ObjectAnimator oa = ObjectAnimator.ofFloat(view, "translationX", 0, 100);
//        oa.setDuration(500);
//        oa.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//            @Override
//            public void onAnimationUpdate(ValueAnimator animation) {
//                //动画在执行的过程当中，不断地调用此方法
//                //animation.getAnimatedFraction()//百分比
//                //得到duration时间内 values当中的某一个中间值。0f~100f
//                float value = (float) animation.getAnimatedValue();//
//                mImg.setScaleX(0.5f + value / 200);//0.5~1
//                mImg.setScaleY(0.5f + value / 200);//0.5~1
//            }
//        });
//        oa.start();

        //方式二，使用ValueAnimator
//        ValueAnimator animator = ValueAnimator.ofFloat(0f, 200f);
//        animator.setDuration(200);
//        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//            @Override
//            public void onAnimationUpdate(ValueAnimator animation) {
//                //动画在执行的过程当中，不断地调用此方法
//                //animation.getAnimatedFraction()//百分比
//                //得到duration时间内 values当中的某一个中间值。0f~100f
//                float value = (float) animation.getAnimatedValue();//
//                mImg.setScaleX(0.5f + value / 200);//0.5~1
//                mImg.setScaleY(0.5f + value / 200);//0.5~1
//            }
//        });
//        animator.start();

        //方式三
        //float... values:代表关键帧的值
//        PropertyValuesHolder holder1 = PropertyValuesHolder.ofFloat("alpha", 1f, 0.5f, 1f);
//        PropertyValuesHolder holder2 = PropertyValuesHolder.ofFloat("scaleX", 1f, 0.5f, 1f);
//        PropertyValuesHolder holder3 = PropertyValuesHolder.ofFloat("scaleY", 1f, 0.5f, 1f);
//
//        ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(view, holder1, holder2, holder3);
//        animator.setDuration(1000);
//        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//            @Override
//            public void onAnimationUpdate(ValueAnimator animation) {
//                float animatedValue = (float) animation.getAnimatedValue();
//                float animatedFraction = animation.getAnimatedFraction();
//                long playTime = animation.getCurrentPlayTime();
//                System.out.println("animatedValue:" + animatedValue + ",  playTime:" + playTime);
//            }
//        });
//        animator.start();

        //方式四  使用动画集合
//        ObjectAnimator animator1 = ObjectAnimator.ofFloat(view, "alpha", 1f, 0.7f, 1f);
//        ObjectAnimator animator2 = ObjectAnimator.ofFloat(view, "scaleX", 1f, 0.7f, 1f);
//        ObjectAnimator animator3 = ObjectAnimator.ofFloat(view, "scaleY", 1f, 0.7f, 1f);
//
//        AnimatorSet animatorSet = new AnimatorSet();
//        animator1.setDuration(500);
////        //同时执行
////        animatorSet.playTogether(animator1, animator2, animator3);
//        //依次执行
//        animatorSet.playSequentially(animator1, animator2, animator3);
//        animatorSet.start();

        //案例：实现抛物线效果
//        /**
//         * x:匀速
//         * y:加速度   y=1/2*g*t*t
//         * 使用估值器最好实现。
//         */
//        ValueAnimator valueAnimator = new ValueAnimator();
//        valueAnimator.setDuration(4000);
//        valueAnimator.setObjectValues(new PointF(0, 0));
//        //估值器---定义计算规则
//        valueAnimator.setEvaluator(new TypeEvaluator<PointF>() {
//
//            @Override
//            public PointF evaluate(float fraction, PointF startValue, PointF endValue) {
//                //拿到第一个点的坐标
//                PointF pointF = new PointF();
//                //x=v*t (s秒)
//                pointF.x = 100f * (fraction * 4);
//                //y=1/2* g * t *t
////                pointF.y = 0.5f * 9.8f * (fraction * 4) * (fraction * 4);
//                pointF.y = 0.5f * 150f * (fraction * 4) * (fraction * 4);
//
//                return pointF;
//            }
//        });
//        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//            @Override
//            public void onAnimationUpdate(ValueAnimator animation) {
//                //得到此时间点的坐标
//                PointF pointF = (PointF) animation.getAnimatedValue();
//                mImg.setX(pointF.x);
//                mImg.setY(pointF.y);
//            }
//        });
//
//        valueAnimator.start();

        ObjectAnimator oa = ObjectAnimator.ofFloat(view, "translationY", 0F, 2000F);
        oa.setDuration(1000);
        //设置加速器
        //一直加速
//        oa.setInterpolator(new AccelerateInterpolator());
        //先加速，后减速
//         oa.setInterpolator(new AccelerateDecelerateInterpolator());
        //先向上弹在网下
//        oa.setInterpolator(new AnticipateInterpolator());
        //先滑动到最终位置网下点在恢复到最终位置
//        oa.setInterpolator(new OvershootInterpolator());
        //循环执行4次
//        oa.setInterpolator(new CycleInterpolator(4));
        //类似弹簧效果
        oa.setInterpolator(new BounceInterpolator());
        oa.start();

    }
}
